<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class BorrowModel extends Model
{
    protected $table = 'borrow';
    public $timestamps = false;

    public function getBookBorrowByUser($userid)
    {
        return $this->where('borrowuser', $userid)->get();
    }

    //添加借书记录
    public function addBorrow($bookid, $userid)
    {
        try {
            $this->insert([
                'borrowuser'    => $userid,
                'bookid'        => $bookid,
                'borrowdate'    => time(),
                'rentdate'      => time()+26784000,
                'last'          => 1,
            ]);
        } catch (\PDOException $e) {
            $e->getMessage();
            return false;
        }
        return true;
    }

    /**
     * @param $borrowid
     * @return bool
     * 删除借书列表
     */
    public function deleteBorrow($borrowid)
    {
        try {
            $this->where('borrowid', $borrowid)->delete();
        } catch (\PDOException $e) {
            $e->getMessage();
            return false;
        }
        return true;
    }

    public function editlastBorrow($borrowid)
    {
        try {
            $this->where('borrowid',$borrowid)->update([
                'rentdate' => time()+2678400,
                'last' => 0,
            ]);
        } catch (\PDOException $e) {
            $e->getMessage();
            return false;
        }
        return true;
    }
}
